#include "Entity.h"

#include "Log.h"
#include "World.h"
#include "DirectXStuff.h"
#include "MatrixStack.h"

Entity::Entity()
{
	Log("New Entity Created");

	//Set our matrix to the identity
	m_data.data.Matrix.m[0][1] = m_data.data.Matrix.m[0][2] = m_data.data.Matrix.m[0][3] =
    m_data.data.Matrix.m[1][0] = m_data.data.Matrix.m[1][2] = m_data.data.Matrix.m[1][3] =
    m_data.data.Matrix.m[2][0] = m_data.data.Matrix.m[2][1] = m_data.data.Matrix.m[2][3] =
    m_data.data.Matrix.m[3][0] = m_data.data.Matrix.m[3][1] = m_data.data.Matrix.m[3][2] = 0.0f;
    m_data.data.Matrix.m[0][0] = m_data.data.Matrix.m[1][1] = m_data.data.Matrix.m[2][2] = m_data.data.Matrix.m[3][3] = 1.0f;

	SetCube();

	//CHRIS::MY GOD THIS IS HORRIBLY CONFUSING
}

void Entity::SetCube()
{
	m_Cube = new CubeVolume(D3DXVECTOR3(m_data.data.Matrix.m[3][0], m_data.data.Matrix.m[3][1], m_data.data.Matrix.m[3][2]), 1.0f, 1.0f, 1.0f);
}

void Entity::Render()
{
	//Set the world matrix and pass the final matrix to the shader
	MatrixStack::ReturnInstance()->SetWorldMatrix(m_data.data.Matrix);//PUSH THE MATRIX
	g_CubeShader->SetMatrix("g_mWorldViewProjection", &MatrixStack::ReturnInstance()->ReturnTop());

	UINT cpass;
	g_CubeShader->Begin(&cpass, 0);
	g_CubeShader->BeginPass(0);

	//Draw the mesh using the default shader
	m_Mesh->Render( g_pd3dDevice );

	g_CubeShader->EndPass();
	g_CubeShader->End();
}

void Entity::MovePos(float _x, float _y, float _z)
{
	m_data.data.Matrix.m[3][0] += _x;
	m_data.data.Matrix.m[3][1] += _y;
	m_data.data.Matrix.m[3][2] += _z;
}

void Entity::setPosition(float _x, float _y, float _z)
{
	m_data.data.Matrix.m[3][0] = _x;
	m_data.data.Matrix.m[3][1] = _y;
	m_data.data.Matrix.m[3][2] = _z;
}

void Entity::Update(float _DeltaT)
{
	if(m_Cube != NULL)
	{
		m_Cube->m_Matrix.m[3][0] = m_data.data.Matrix.m[3][0];
		m_Cube->m_Matrix.m[3][1] = m_data.data.Matrix.m[3][1];
		m_Cube->m_Matrix.m[3][2] = m_data.data.Matrix.m[3][2];
	}
}